var dado;


function obterDadosEspaciais(func, filtro){        
       
    $.ajax({
        type: "POST",
        url: "php/src/control/control.multipoligono.php",
        data: ({
            func: func,
            filtro: filtro,
            extensao: extensaoHistorico[extensaoHistorico.length-1]
        }),
        dataType: "json",
        context: document.body,              
          
        success: function(data){          
            desenhaMultiPoligono(func, data);
        },
        beforeSend: function () {
            $('#loading').show();
        },
        complete: function () {
            $('#loading').hide();
        } 
    });
}

function desenhaMultiPoligono(func, data){      

    var multipoligono;

    if(func == "obterDadosEspaciais" || func == "obterDadosEspaciaisRaster"){
        context.fillStyle = "#F5DEB3";
    }
    if(func == "marcarPonto"){
        document.getElementById("municipio").innerHTML = data[0].municipio;
        context.fillStyle = "#F4A460";
    }
    if(func == "zoomPonto"){
        document.getElementById("municipio").innerHTML = data[0].municipio;
    }
    
    if((func == "zoomSelecao") || (func == "zoomPonto")){
        canvas.width = canvas.width;
        context.fillStyle = "#F5DEB3";
    }
    
    for (var j in data){
        if(j == 0 && func == "zoomSelecao"){
            extensaoHistorico.push(data[j]);
        }else{
            multipoligono = data[j];
            for(var i in multipoligono.poligonos){
                desenhaPoligono(multipoligono.poligonos[i]);
            }
        }
    }
}

function desenhaPoligono(poligono){
    
    context.beginPath();
    context.lineWidth = 0.3;
    for (var i in poligono.pontos){
      
        if(i == 0){            
            context.moveTo(poligono.pontos[i].x,poligono.pontos[i].y);  
        }
        else{
            context.lineTo(poligono.pontos[i].x,poligono.pontos[i].y);
        }
    }
    context.fill();
    context.stroke();                        
}

function desenhaPoligonoRegion(poligono){
 
    contextRaster.strokeStyle = "#F00";
    contextRaster.beginPath();
    contextRaster.lineWidth = 5;

    for (var i in poligono.pontos){
      
        if(i == 0){            

            contextRaster.moveTo(poligono.pontos[i].x,poligono.pontos[i].y);  
        }
        else{
            contextRaster.lineTo(poligono.pontos[i].x,poligono.pontos[i].y);
        }
    }
    contextRaster.stroke();
}

function onLoadComboBox(id){
              
    $.ajax({
        type: "POST",
        url: "php/src/control/control.raster.php",
        data: ({
            func: "list"
        }),
        dataType: "json",
        context: document.body,              
          
        success: function(data){      
            for (var i in data){         
                $(id).addOption(data[i].raster, i, false);
            }
        }        
    });
}

function obterRaster(func, filtro){        
       
    $.ajax({
        type: "POST",
        url: "php/src/control/control.raster.php",
        data: ({
            func: func,
            filtro: filtro
        }),
        dataType: "json",
        context: document.body,              
          
        success: function(data){

            dado = data;
            
            if(func == "obterRaster"){
                desenhaGrid(data,0.1);    
            }
            if(func == "obterGeoRaster"){
                desenhaGridGeo(data);
            }
            if(func == "obterGeoRasters"){
                canvasRaster.width = 550;
                canvasRaster.height = 550;
                
                for(var i in data){
                    desenhaGeoGrid(data[i]);
                }                
            }            
        }
        ,
        beforeSend: function () {
            $('#loading').show();
        },
        complete: function () {
            $('#loading').hide();
        }         
    });
}

function desenhaGeoGrid(data){    
            
    var numRows = data.dy;
    var numCols = data.dx;    
    
    var weight = data.weight;
    var colors = weight.split(" ");
    var color = 0;
    
    var cel = data.celula;
    var xpos = cel.xmin; 
    var ypos = cel.ymin;
    
    var size = cel.xmax - cel.xmin;

    for(var i=0; i<numRows; i++){ 
        for(var j=1; j<=numCols; j++){
            
            defineCores4CRS(colors[color]);
            if(colors[color] != 0){
                contextRaster.fillRect(xpos,ypos,size,size);
            }
            xpos+=size;
            color++;
        }
        ypos-=size;
        xpos = cel.xmin;
    }
    desenhaPoligonoRegion(data.poligono[0]);    
}

function desenhaGrid(data,scale){
    
    var weight = data.weight;    
    var colors = weight.split(" ");  
    var size = data.size;
    var numRows = data.dy;
    var numCols = data.dx; 
    

    canvasRaster.width = (data.dx * size)*scale;
    canvasRaster.height = (data.dy * size)*scale;

    contextRaster.scale(scale,scale);
    

    var xpos = 0; 
    var ypos = (data.dy * size) - size;        
    var color = 0;


    for(var i=0; i<=numRows; i++){
        for(var j=1; j<=numCols; j++){
            defineCores4CRS(colors[color++]);
            contextRaster.fillRect(xpos,ypos,size,size);
            contextRaster.strokeRect(xpos,ypos,size,size);
            xpos+=size;
        }
        ypos-=size;
        xpos = 0;
    }    
    
    contextRaster.strokeStyle = "#F00";
    desenhaPoligonoRegion(data.poligono[0]);
}

var grau=0.1;

function zoom(tipo){
    
    if(tipo == '-'){
        grau-=0.1;
    }else{
        grau+=0.1;
    }
    desenhaGrid(dado,grau);        
}

function mostrarDados(data){    
    $("#dx").text(data.dx);
    $("#dy").text(data.dy);
}

function defineCores4CRS(weight){

    contextRaster.lineWidth = 1;   
    contextRaster.strokeStyle = "#000";
    
    if(weight == 0){
        contextRaster.fillStyle = "#FFF";
    }
    if(weight == 1){
        contextRaster.fillStyle = "#CFCFCF";
    }
    if(weight == 2){
        contextRaster.fillStyle = "#828282";
    }
    if(weight == 3){
        contextRaster.fillStyle = "#000";
    }    
}

